package com.pro.service.imple;

import com.pro.mapper.JsGoodsMapper;
import com.pro.pojo.*;
import com.pro.service.JsGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author cyj
 * @date 2022.1.10$ 14.07$
 * @content :
 */

@Service
public class JsGoodsServiceImple implements JsGoodsService {

    private Integer updateId;
    @Autowired
    private JsGoodsMapper mapper;
    @Override
    public int count(JsGoods jsGoods) {
        return mapper.count(jsGoods);
    }

    @Override
    public List<JsGoods> getTable(JsGoods jsGoods) {
        List<JsGoods> goods = mapper.getTable(jsGoods);
        updateId = jsGoods.getUpdateId();
        if(goods != null){
            JsOperationLog jsOperationLog = new JsOperationLog();
            jsOperationLog.setUpdateId(updateId);
            jsOperationLog.setOperationType(3);
            jsOperationLog.setExecutionTime(new Date());
            jsOperationLog.setOperationList("查看商品列表");
            jsOperationLog.setType("1");
            Integer oper=mapper.addOperationLog(jsOperationLog);
        }
        return goods;
    }

    /*根据按钮修改上下架状态     2022.1.11   15.51     CYJ*/
    @Override
    public Boolean updateGoodsSate(Integer productId,Integer productSate) {
        return mapper.updateGoodsSate(productId,productSate);
    }

    /*查看某个商品的详细审核信息 2022.1.11   16.44     CYJ*/
    @Override
    public List<JsCourseRecord> getGoodsRecord(Integer productId) {
        return mapper.getGoodsRecord(productId);
    }

    /*删除一个商品               2022.1.11   19.35     CYJ*/
    @Override
    public Boolean delGoods(Integer productId) {
        Boolean flag = mapper.delGoods(productId);
        if(flag != false){
            JsOperationLog jsOperationLog = new JsOperationLog();
            jsOperationLog.setUpdateId(updateId);
            jsOperationLog.setOperationType(4);
            jsOperationLog.setExecutionTime(new Date());
            jsOperationLog.setOperationList("删除一件商品");
            jsOperationLog.setType("1");
            mapper.addOperationLog(jsOperationLog);
        }
        return flag;
    }

    /*高级检索商品               2022.1.12   9.34     CYJ*/
    @Override
    public List<JsGoods> SearchGoods(JsGoods jsGoods) {
        return mapper.SearchGoods(jsGoods);
    }

    /*查看一个商品的详细信息     2022.1.12   11.29     CYJ*/
    @Override
    public JsGoods getGoodsMsg(Integer productId) {
        JsGoods good = mapper.getGoodsMsg(productId);
        if(good != null){
            JsOperationLog jsOperationLog = new JsOperationLog();
            jsOperationLog.setUpdateId(updateId);
            jsOperationLog.setOperationType(3);
            jsOperationLog.setExecutionTime(new Date());
            jsOperationLog.setOperationList("查看一件商品的详情");
            jsOperationLog.setType("1");
            mapper.addOperationLog(jsOperationLog);
        }
        return good;
    }

    /*修改一件商品的详细信息     2022.1.12   14.32     CYJ*/
    @Override
    public Boolean updateGoods(JsGoods jsGoods) {
        Boolean flag = mapper.updateGoods(jsGoods);
        if(flag != false){
            JsOperationLog jsOperationLog = new JsOperationLog();
            jsOperationLog.setUpdateId(updateId);
            jsOperationLog.setOperationType(2);
            jsOperationLog.setExecutionTime(new Date());
            jsOperationLog.setOperationList("修改商品列表");
            jsOperationLog.setType("1");
            mapper.addOperationLog(jsOperationLog);
        }
        //判断价格是否变化,进行历史价格日志的添加
        if (jsGoods.getOriginalpriceProduct() != jsGoods.getCurrentpriceProduct()){
            JsGoods product = mapper.getGoodsMsg(jsGoods.getProductId());
            JsGoodsJournal jsGoodsJournal = new JsGoodsJournal();
            jsGoodsJournal.setCourseId(jsGoods.getProductId());
            jsGoodsJournal.setCourseName(jsGoods.getProductName());
            jsGoodsJournal.setCourseStatus(jsGoods.getProductSate().toString());
            jsGoodsJournal.setUserId(updateId);
            jsGoodsJournal.setUpdateDate(new Date());
            jsGoodsJournal.setOriginalpriceProduct(product.getOriginalpriceProduct());
            jsGoodsJournal.setCurrentpriceProduct(jsGoods.getCurrentpriceProduct());
            mapper.addJournal(jsGoodsJournal);
        }
        return flag;
    }

    /*查看商品的日志             2022.1.12   16.28     CYJ*/
    @Override
    public List<JsCourse> getGoodsJournal(Integer productId, Integer type) {
        List<JsCourse> operationLog = mapper.getGoodsJournal(productId,type);
        if(operationLog != null){
            JsOperationLog jsOperationLog = new JsOperationLog();
            jsOperationLog.setUpdateId(updateId);
            jsOperationLog.setOperationType(3);
            jsOperationLog.setExecutionTime(new Date());
            jsOperationLog.setOperationList("查看一件商品的日志");
            jsOperationLog.setType("1");
            mapper.addOperationLog(jsOperationLog);
        }
        return operationLog;
    }

    /**
     * @author CYJ
     * @date 2022/1/13  18:49
     * @content : 查看商品的相关操作日志
     */
    @Override
    public List<JsOperationLog> getGoodsJournals(String type, String executionTime, Integer operationType) {
        return mapper.getGoodsJournals(type,executionTime,operationType);
    }
    /**
     * @author CYJ
     * @date 2022/1/13  19:44
     * @content : 删除一条日志
     */
    @Override
    public Boolean delJournalById(Integer id) {
        return mapper.delJournalById(id);
    }

    /**
     *
     * @author CYJ
     * @date 2021/12/14 2002.8.54
     * @content :获取商品历史价格记录
     */
    @Override
    public List<JsCourseJournal> getPriceJournal(Integer productCode, String productName, String updateDate, Integer type) {
        return mapper.getPriceJournal(productCode,productName,updateDate,type);
    }

    /**
     *
     * @author CYJ
     * @date 2021/12/14 2002.9.15
     * @content :删除一条商品历史价格记录
     */
    @Override
    public Boolean delPriceJournal(Integer id) {
        return mapper.delPriceJournal(id);
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.19  18.53
     * @content :获取商品的分类
     */
    @Override
    public List<JsGoodsCategroy> getGoodsClass() {
        return mapper.getGoodsClass();
    }
    /**
     *
     * @author CYJ
     * @date 2022/1.19  19.12
     * @content :添加商品
     */
    @Override
    public Boolean addGoods(JsProduct jsProduct) {
        return mapper.addGoods(jsProduct);
    }
    /**
     *
     * @author CYJ
     * @date 2022/1.22  18.35
     * @content :获取审核商品信息
     */
    @Override
    public List<JsGoods> getExamineTable(String productName,String title,String trueName,Integer type,String result) {
        return mapper.getExamineTable(productName,title,trueName,type,result);
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.26  22.39
     * @content :审核商品
     */
    @Override
    public Boolean insertRecord(JsCourseRecord jsCourseRecord) {
        Boolean flag1=mapper.insertRecord(jsCourseRecord);
        Boolean flag2=mapper.insertRecord2(jsCourseRecord.getCourseName(),jsCourseRecord.getId());
        if(flag1==true&&flag2==true){
            return flag2;
        }else{
            return flag1;
        }

    }

    /**
     *
     * @author CYJ
     * @date 2022/1.28  20.37
     * @content :商品点击数
     */
    @Override
    public List<JsGoods> hitsSum() {
        return mapper.hitsSum();
    }

    /**
     *
     * @author CYJ
     * @date 2022/1.28  20.55
     * @content :商品收藏数
     */
    @Override
    public List<JsGoods> collectionSum() {
        return mapper.collectionSum();
    }
}
